/*
 * @title: 捕获vue异常全局工具
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-04-03 11:06:33
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-04-06 16:10:20
 * @version: 1.0.3
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
 */

import Vue from 'vue';
import store from '@/store';
import { isString, isArray } from '@/utils';
import AppConfig from '@/appconfig';

const { errorLog: needErrorLog } = AppConfig;

/**
 * @todo: 判断是否需要异常日志捕获
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-04-03 11:07:48
 */
function checkNeed() {
  const env = process.env.NODE_ENV;
  if (isString(needErrorLog))
    return env === needErrorLog;

  if (isArray(needErrorLog))
    return needErrorLog.includes(env);

  return false;
}

if (checkNeed())
  // err指代error对象，info是一个Vue特有的字符串，vm指代Vue应用本身
  Vue.config.errorHandler = function(err, vm, info, a) {
    Vue.nextTick(() => {
      store.dispatch('errorLog/addErrorLog', {
        err,
        vm,
        info,
        url: window.location.href
      });
      console.error(err, info);
    });
  };

